<?php

/**
 * This is the model class for table "{{words}}".
 *
 * The followings are the available columns in table '{{words}}':
 * @property integer $zw_id
 * @property string $zw_title
 * @property integer $zw_level
 * @property string $zw_replaceword
 * @property integer $zw_listorder
 * @property integer $inputtime
 * 张梦林
 * 2012-12-5
 */
class Words extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Words the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{words}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zw_title, inputtime', 'required'),
			array('zw_level, zw_listorder, inputtime', 'numerical', 'integerOnly'=>true),
			array('zw_title', 'length', 'max'=>100),
			array('zw_replaceword', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zw_id, zw_title, zw_level, zw_replaceword, zw_listorder, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zw_id' => 'ID',
			'zw_title' => '关键词',
			'zw_level' => '级别',
			'zw_replaceword' => '替换文字',
			'zw_listorder' => '排序',
			'inputtime' => '开始时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zw_id',$this->zw_id);
		$criteria->compare('zw_title',$this->zw_title,true);
		$criteria->compare('zw_level',$this->zw_level);
		$criteria->compare('zw_replaceword',$this->zw_replaceword,true);
		$criteria->compare('zw_listorder',$this->zw_listorder);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>'12',		
			),
		));
	}
	//屏蔽词级别
	public function getLevel($type=null){
		if($type==null){
			return array(
					'1'=>'替换',
					'2'=>'禁止',
			);
		}else{
			$level = $this->getLevel();
			return $level[$type];
		}
	}
	//保存前操作
	public function beforeSave(){
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
			}
			return true;
		}
		return true;
	}
}
?>